package com.luomo.java.juc;

import java.util.concurrent.*;

/**
 * @author LiuMei
 * @date 2021-04-13.
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();
//        ExecutorService threadPool = Executors.newCachedThreadPool();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());
        threadPoolExecutor.allowCoreThreadTimeOut(false);
        try {
            for (int i = 0; i < 10; i++) {
//                try {
//                    TimeUnit.SECONDS.sleep(1);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                threadPoolExecutor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t办理业务");
                });
//                System.out.println(threadPoolExecutor.getPoolSize() + "=====");
            }
//            try {
//                TimeUnit.SECONDS.sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(threadPoolExecutor.getPoolSize() + "=====");
        } finally {
            threadPoolExecutor.shutdown();
        }
    }

}
